﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QualityRpt.Dao;
using log4net;
using System.Data;
using QualityRpt.Utils;
using System.Threading.Tasks;

namespace QualityRpt.Service
{
    /// <summary>
    /// 漏读相关服务类
    /// </summary>
    public class UNReadTransService:BaseService
    {
        static ILog logger = LogManager.GetLogger("UNReadTransService");
        private static IUNReadTransDao _impDao = null;
        static UNReadTransService()
        {
            _impDao = CreateDaoInstance("IUNReadTransDao") as IUNReadTransDao;
        }
        /// <summary>
        /// 抽取一段时间的漏读数据
        /// </summary>
        public static void Extract(DateTime startTime, DateTime endTime, out string msg)
        {
            _impDao.Extract(startTime, endTime, out msg);
        }
        /// <summary>
        /// 对漏读数据执行统计
        /// </summary>
        public static void StatTransCount(DateTime startTime, DateTime endTime)
        {
            _impDao.StatTransCount(startTime, endTime);
        }
        /// <summary>
        /// 漏读标签综合查询支持
        /// </summary>
        public static DataTable GetOBUStatByQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin, string cnin,
            string rdin, string plazain, string lanein, bool fromTemp, string[] numstartArray, string[] numendArray)
        {
            return _impDao.GetOBUStatByQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plazain, lanein, fromTemp, numstartArray, numendArray);
        }
        /// <summary>
        /// 漏读天线综合查询支持
        /// </summary>
        public static DataTable GetAIRStatByQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin, string rdin, string plazain, string lanein)
        {
            return _impDao.GetAIRStatByQ(startTime, endTime, statTType, spin, specin, rdin, plazain, lanein);
        }
        /// <summary>
        /// obu交易明细
        /// </summary>
        public static DataTable GetOBUDetailByQ(DateTime startTime, DateTime endTime, string spin, string specin, string cnin, string rdin,
            string plazain, string lanein, bool fromTempTable, string[] numstartArray, string[] numendArray, PageUtils pu)
        {
            return _impDao.GetOBUDetailByQ(startTime, endTime, spin, specin, cnin, rdin, plazain, lanein, fromTempTable, numstartArray, numendArray, pu);
        }
        /// <summary>
        /// 以标签号分组的明细记录v1.2
        /// </summary>
        public static DataTable GetOBUGDetailByQ(DateTime startTime, DateTime endTime, string spin, string specin, string cnin, string rdin,
            string plazain, string lanein, bool fromTempTable, string[] numstartArray, string[] numendArray, PageUtils pu)
        {
            DataTable unobuTable = _impDao.GetOBUGDetailByQ(startTime, endTime, spin, specin, cnin, rdin, plazain, lanein, fromTempTable, numstartArray, numendArray, pu);
            DataView dv = new DataView(unobuTable);
            DataTable obunotable = dv.ToTable(true, "OBUNO");
            StringBuilder sbobuno = new StringBuilder();
            for (int i = 0; i < obunotable.Rows.Count; i++)
            {
                if (i > 0)
                {
                    sbobuno.Append(",");
                }
                sbobuno.Append(obunotable.Rows[i]["OBUNO"].ToString());
            }
            DataTable obuTable = TransNoteService.GetOBUGDetailByQ(startTime, endTime, sbobuno.ToString(), rdin, plazain, lanein);
            List<string> columnNames = new List<string>();
            for (int i = 0; i < unobuTable.Columns.Count - 1; i++)
            {
                columnNames.Add(unobuTable.Columns[i].ColumnName);
            }
            DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
            resultTable.Columns.Add("UNREAD_TRANSCOUNT", typeof(int));
            resultTable.Columns.Add("TRANSCOUNT", typeof(int));
            resultTable.Columns.Add("SUM_TRANSCOUNT", typeof(int), "UNREAD_TRANSCOUNT+TRANSCOUNT");
            resultTable.Columns.Add("UNREAD_RATE", typeof(decimal), "iif(TRANSCOUNT>0,UNREAD_TRANSCOUNT/SUM_TRANSCOUNT,null)");
            StringBuilder selText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                selText.Clear(); DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < columnNames.Count; j++)
                {
                    string clName = columnNames[j];
                    DataColumn _dtc = resultTable.Columns[clName];
                    if (j > 0)
                    {
                        selText.Append(" AND ");
                    }
                    if (_dtc.DataType.Equals(typeof(string)))
                    {
                        selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                    }
                    else
                    {
                        selText.Append(clName + "=" + curRow[clName].ToString());
                    }
                }
                DataRow[] lbSelRows = unobuTable.Select(selText.ToString());
                DataRow[] SelRows = obuTable.Select(selText.ToString());
                if (lbSelRows.Length > 0)
                {
                    curRow["UNREAD_TRANSCOUNT"] = lbSelRows[0]["TRANSCOUNT"];
                }
                if (SelRows.Length > 0)
                {
                    curRow["TRANSCOUNT"] = SelRows[0]["TRANSCOUNT"];
                }
            }
            return resultTable;
        }
        /// <summary>
        /// 天线交易明细
        /// </summary>
        public static DataTable GetAIRDetailByQ(DateTime startTime, DateTime endTime, string spin, string specin,
            string rdin, string plazain, string lanein, PageUtils pu)
        {
            return _impDao.GetAIRDetailByQ(startTime, endTime, spin, specin, rdin, plazain, lanein, pu);
        }
        /// <summary>
        /// 天线分组明细
        /// </summary>
        public static DataTable GetAIRGDetailByQ(DateTime startTime, DateTime endTime, string spin, string specin,
            string rdin, string plazain, string lanein, PageUtils pu)
        {
            DataTable unAirTable = _impDao.GetAIRGDetailByQ(startTime, endTime, spin, specin, rdin, plazain, lanein, pu);
            DataView dv = new DataView(unAirTable);
            DataTable airLane = dv.ToTable(true, "PLAZAID", "LANENO");
            StringBuilder sbPlaza = new StringBuilder(); 
            StringBuilder sbLane = new StringBuilder();
            for (int i = 0; i < airLane.Rows.Count; i++)
            {
                if (i > 0)
                {
                    sbPlaza.Append(","); sbLane.Append(",");
                }
                DataRow airRow = airLane.Rows[i];
                sbPlaza.Append(airRow["PLAZAID"].ToString()); sbLane.Append(airRow["LANENO"].ToString());
            }
            DataTable airTransTable = TransNoteService.GetAIRGDetailByQ(startTime, endTime, sbPlaza.ToString(), sbLane.ToString());
            List<string> columnNames = new List<string>();
            for (int i = 0; i < unAirTable.Columns.Count - 1; i++)
            {
                columnNames.Add(unAirTable.Columns[i].ColumnName);
            }
            DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
            resultTable.Columns.Add("UNREAD_TRANSCOUNT", typeof(int));
            resultTable.Columns.Add("TRANSCOUNT", typeof(int));
            resultTable.Columns.Add("SUM_TRANSCOUNT", typeof(int), "UNREAD_TRANSCOUNT+TRANSCOUNT");
            resultTable.Columns.Add("UNREAD_RATE", typeof(decimal), "iif(TRANSCOUNT>0,UNREAD_TRANSCOUNT/SUM_TRANSCOUNT,null)");
            StringBuilder selText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                selText.Clear(); DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < columnNames.Count; j++)
                {
                    string clName = columnNames[j];
                    DataColumn _dtc = resultTable.Columns[clName];
                    if (j > 0)
                    {
                        selText.Append(" AND ");
                    }
                    if (_dtc.DataType.Equals(typeof(string)))
                    {
                        selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                    }
                    else
                    {
                        selText.Append(clName + "=" + curRow[clName].ToString());
                    }
                }
                DataRow[] lbSelRows = unAirTable.Select(selText.ToString());
                DataRow[] SelRows = airTransTable.Select(selText.ToString());
                if (lbSelRows.Length > 0)
                {
                    curRow["UNREAD_TRANSCOUNT"] = lbSelRows[0]["TRANSCOUNT"];
                }
                if (SelRows.Length > 0)
                {
                    curRow["TRANSCOUNT"] = SelRows[0]["TRANSCOUNT"];
                }
            }
            return resultTable;
        }
        /// <summary>
        /// 从中间表汇总obu漏读数据
        /// </summary>
        public static DataTable GetOBUStatByMQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin,
           string cnin, string rdin, string plin, string lnin)
        {
            return _impDao.GetOBUStatByMQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plin, lnin);
        }
         /// <summary>
        /// 从中间表汇总天线漏读数据
        /// </summary>
        public static DataTable GetAIRStatByMQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin,
           string rdin, string plin, string lnin)
        {
            return _impDao.GetAIRStatByMQ(startTime, endTime, statTType, spin, specin, rdin, plin, lnin);
        }
		public static DataTable GetJZTBG(DateTime startTime,DateTime endTime)
		{
			return _impDao.GetJZTBG (startTime, endTime);
		}
    }
}
